package com.hama.iotrust.common.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.codehaus.jackson.annotate.JsonIgnore;

import com.hama.iotrust.common.ui.EasyUITreeGridRow;

public class EasyUITreeGridRow extends HashMap<String, Object> {

    private static final long serialVersionUID = -4050845880991231667L;

    public enum State {
        OPEN("open"), CLOSED("closed"), NULL("");

        private String state;

        State(String state){
            this.state = state;
        }

        public String getState() {
            return state;
        }

        public String toString() {
            return getState();
        }
    }

    private String                  id;

    @JsonIgnore
    private String                  parentId;
    private String                  text;
    private String                  iconCls;
    private State                   state;
    private boolean                 checked;
    private List<EasyUITreeGridRow> children;

    public EasyUITreeGridRow(String id, String parentId, String text){
        this.id = id;
        put("id", id);
        this.parentId = parentId;
        this.text = text;
        put("text", text);
    }

    public EasyUITreeGridRow getChildNodeById(String id) {
        if (children != null && children.size() > 0) {
            for (EasyUITreeGridRow subNode : children) {
                if (subNode.getId().equals(id)) {
                    return subNode;
                }
                EasyUITreeGridRow foundSubNode = subNode.getChildNodeById(id);
                if (foundSubNode != null) return foundSubNode;
            }
        }
        return null;
    }

    public void removeNode(String id) {
        if (children != null && children.size() > 0) {
            for (int i = children.size() - 1; i >= 0; i--) {
                EasyUITreeGridRow subNode = children.get(i);
                if (subNode.getId().equals(id)) {
                    children.remove(subNode);
                }
                subNode.removeNode(id);
            }
        }
    }

    public void addChildNode(EasyUITreeGridRow childNode) {
        if (children == null) children = new ArrayList<EasyUITreeGridRow>();
        children.add(childNode);
        put("children", children);
        if (this.children != null && this.children.size() > 0) put("state", this.state.toString());
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
        put("id", id);
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
        put("text", text);
    }

    public State getState() {
        if (children == null || children.size() == 0) {
            return State.NULL;
        }
        return state;
    }

    public void setState(State state) {
        this.state = state;
        if (state == null) this.state = State.NULL;
    }

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
        put("checked", checked);
    }

    public List<EasyUITreeGridRow> getChildren() {
        return children;
    }

    public void setChildren(List<EasyUITreeGridRow> children) {
        this.children = children;
        put("children", children);
        if (this.children != null && this.children.size() > 0) put("state", this.state.toString());
    }

    public String getIconCls() {
        return iconCls;
    }

    public void setIconCls(String iconCls) {
        this.iconCls = iconCls;
        put("iconCls", iconCls);
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

}
